package p270jp.pxv.android.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import p018ai.C0218e;
import p270jp.pxv.android.R;
import p274k2.ContextCompat;

/* renamed from: jp.pxv.android.view.PixivCircleProgressBar */
/* loaded from: classes2.dex */
public class PixivCircleProgressBar extends View {

    /* renamed from: a */
    public Context f19628a;

    /* renamed from: b */
    public int f19629b;

    /* renamed from: c */
    public int f19630c;

    /* renamed from: d */
    public int f19631d;

    /* renamed from: e */
    public int f19632e;

    /* renamed from: f */
    public int f19633f;

    /* renamed from: g */
    public int f19634g;

    /* renamed from: h */
    public int f19635h;

    /* renamed from: i */
    public int f19636i;

    /* renamed from: j */
    public int f19637j;

    /* renamed from: q */
    public Paint.FontMetrics f19644q;

    /* renamed from: v */
    public HandlerC4389a f19649v;

    /* renamed from: k */
    public Paint f19638k = new Paint();

    /* renamed from: l */
    public Paint f19639l = new Paint();

    /* renamed from: m */
    public Paint f19640m = new Paint();

    /* renamed from: n */
    public Paint f19641n = new Paint();

    /* renamed from: o */
    public Paint f19642o = new Paint();

    /* renamed from: p */
    public Paint f19643p = new Paint();

    /* renamed from: r */
    public RectF f19645r = new RectF();

    /* renamed from: s */
    public RectF f19646s = new RectF();

    /* renamed from: t */
    public RectF f19647t = new RectF();

    /* renamed from: u */
    public RectF f19648u = new RectF();

    /* renamed from: w */
    public List<Bitmap> f19650w = new ArrayList();

    /* renamed from: x */
    public int f19651x = 0;

    /* renamed from: y */
    public float f19652y = 0.0f;

    /* renamed from: jp.pxv.android.view.PixivCircleProgressBar$a */
    /* loaded from: classes2.dex */
    public class HandlerC4389a extends Handler {
        public HandlerC4389a() {
            sendMessageDelayed(obtainMessage(0), 0);
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            PixivCircleProgressBar.this.invalidate();
            PixivCircleProgressBar pixivCircleProgressBar = PixivCircleProgressBar.this;
            if (pixivCircleProgressBar.f19651x < pixivCircleProgressBar.f19650w.size() - 1) {
                PixivCircleProgressBar.this.f19651x++;
            } else {
                PixivCircleProgressBar.this.f19651x = 0;
            }
            removeMessages(0);
            sendMessageDelayed(obtainMessage(0), 250);
        }
    }

    public PixivCircleProgressBar(Context context, AttributeSet attributeSet) {
        super(context, attributeSet, 0, 2131887296);
        this.f19628a = context;
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, C0218e.f749g, 0, 2131887296);
        this.f19629b = obtainStyledAttributes.getInt(7, 100);
        this.f19630c = obtainStyledAttributes.getColor(3, 0);
        this.f19635h = obtainStyledAttributes.getColor(8, 0);
        this.f19636i = obtainStyledAttributes.getDimensionPixelSize(9, m4477a(context, 1));
        this.f19631d = obtainStyledAttributes.getColor(1, 0);
        int dimensionPixelSize = obtainStyledAttributes.getDimensionPixelSize(2, m4477a(context, 2));
        this.f19632e = dimensionPixelSize;
        this.f19633f = (this.f19636i * 2) + dimensionPixelSize;
        this.f19634g = obtainStyledAttributes.getColor(0, 0);
        setupProgressDrawables(obtainStyledAttributes);
    }

    /* renamed from: a */
    public static int m4477a(Context context, int i) {
        return (int) TypedValue.applyDimension(1, (float) i, context.getResources().getDisplayMetrics());
    }

    /* renamed from: b */
    public static Bitmap m4476b(Bitmap bitmap, float f) {
        Matrix matrix = new Matrix();
        matrix.postScale(f, f);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    /* renamed from: c */
    public static Bitmap m4475c(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }
        int intrinsicWidth = drawable.getIntrinsicWidth();
        int i = 1;
        if (intrinsicWidth <= 0) {
            intrinsicWidth = 1;
        }
        int intrinsicHeight = drawable.getIntrinsicHeight();
        if (intrinsicHeight > 0) {
            i = intrinsicHeight;
        }
        Bitmap createBitmap = Bitmap.createBitmap(intrinsicWidth, i, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return createBitmap;
    }

    private void setupProgressDrawables(TypedArray typedArray) {
        Drawable drawable = typedArray.getDrawable(4);
        if (drawable != null) {
            this.f19637j = typedArray.getDimensionPixelSize(5, m4477a(this.f19628a, 0));
            float dimensionPixelSize = (float) typedArray.getDimensionPixelSize(6, m4477a(this.f19628a, 48));
            if (drawable instanceof AnimationDrawable) {
                AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
                for (int i = 0; i < animationDrawable.getNumberOfFrames(); i++) {
                    Bitmap c = m4475c(animationDrawable.getFrame(i));
                    this.f19650w.add(m4476b(c, dimensionPixelSize / ((float) (c.getWidth() > c.getHeight() ? c.getWidth() : c.getHeight()))));
                }
                return;
            }
            Bitmap c2 = m4475c(drawable);
            this.f19650w.add(m4476b(c2, dimensionPixelSize / ((float) (c2.getWidth() > c2.getHeight() ? c2.getWidth() : c2.getHeight()))));
        }
    }

    /* renamed from: d */
    public final void m4474d() {
        int width = getWidth();
        int height = getHeight();
        float f = ((float) this.f19633f) / 2.0f;
        float f2 = (float) width;
        float f3 = f2 - f;
        float f4 = (float) height;
        float f5 = f4 - f;
        this.f19646s = new RectF(f, f, f3, f5);
        this.f19645r = new RectF(f, f, f3, f5);
        float f6 = ((float) this.f19636i) / 2.0f;
        int i = this.f19633f;
        this.f19647t = new RectF(((float) i) - f6, ((float) i) - f6, ((float) (width - i)) + f6, ((float) (height - i)) + f6);
        this.f19648u = new RectF(f6, f6, f2 - f6, f4 - f6);
    }

    /* renamed from: e */
    public final void m4473e() {
        this.f19640m.setTextSize((float) m4477a(this.f19628a, 12));
        Paint paint = this.f19640m;
        Context context = getContext();
        Object obj = ContextCompat.f19963a;
        paint.setColor(ContextCompat.C4473d.m4257a(context, R.color.guideline_text_5));
        this.f19640m.setFlags(1);
        this.f19644q = this.f19640m.getFontMetrics();
        this.f19638k.setStyle(Paint.Style.FILL);
        this.f19638k.setColor(this.f19630c);
        this.f19638k.setFlags(1);
        this.f19641n.setStyle(Paint.Style.STROKE);
        this.f19641n.setColor(this.f19631d);
        this.f19641n.setStrokeWidth((float) this.f19632e);
        this.f19641n.setFlags(1);
        this.f19642o.setStyle(Paint.Style.STROKE);
        this.f19642o.setColor(this.f19634g);
        this.f19642o.setStrokeWidth((float) this.f19633f);
        this.f19642o.setFlags(1);
        this.f19643p.setStyle(Paint.Style.STROKE);
        this.f19643p.setColor(this.f19635h);
        this.f19643p.setStrokeWidth((float) this.f19636i);
        this.f19643p.setFlags(1);
    }

    public int getMax() {
        return this.f19629b;
    }

    @Override // android.view.View
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (this.f19650w != null) {
            this.f19649v = new HandlerC4389a();
        }
    }

    @Override // android.view.View
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        HandlerC4389a aVar = this.f19649v;
        if (aVar != null) {
            aVar.removeMessages(0);
            this.f19649v = null;
        }
    }

    @Override // android.view.View
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(((float) getWidth()) / 2.0f, ((float) getHeight()) / 2.0f, ((float) (getWidth() > getHeight() ? getWidth() : getHeight())) / 2.0f, this.f19638k);
        String format = String.format(Locale.US, "%d%%", Integer.valueOf((int) (this.f19652y * 100.0f)));
        float width = (((float) getWidth()) / 2.0f) - (this.f19640m.measureText(format) / 2.0f);
        Paint.FontMetrics fontMetrics = this.f19644q;
        float height = (((float) getHeight()) / 2.0f) - ((fontMetrics.ascent + fontMetrics.descent) / 2.0f);
        if (this.f19650w.size() != 0) {
            Bitmap bitmap = this.f19650w.get(this.f19651x);
            Paint.FontMetrics fontMetrics2 = this.f19644q;
            float f = ((fontMetrics2.descent - fontMetrics2.ascent) + fontMetrics2.leading) / 2.0f;
            canvas.drawBitmap(bitmap, (((float) getWidth()) / 2.0f) - (((float) bitmap.getWidth()) / 2.0f), (((((float) getHeight()) / 2.0f) - (((float) bitmap.getHeight()) / 2.0f)) - f) - (((float) this.f19637j) / 2.0f), this.f19639l);
            canvas.drawText(format, width, (((float) this.f19637j) / 2.0f) + (((float) bitmap.getHeight()) / 2.0f) + height + f, this.f19640m);
        } else {
            canvas.drawText(format, width, height, this.f19640m);
        }
        canvas.drawArc(this.f19646s, -90.0f, 360.0f, false, this.f19642o);
        canvas.drawArc(this.f19645r, -90.0f, this.f19652y * 360.0f, false, this.f19641n);
        canvas.drawArc(this.f19647t, -90.0f, 360.0f, false, this.f19643p);
        canvas.drawArc(this.f19648u, -90.0f, 360.0f, false, this.f19643p);
    }

    @Override // android.view.View
    public void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        m4473e();
        m4474d();
    }

    public void setProgress(float f) {
        this.f19652y = f / ((float) this.f19629b);
        m4473e();
        m4474d();
        invalidate();
    }
}
